<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <el-alert
            title="三级路由【不缓存路由并且不固定表格高度】的示例"
            type="success"
            :closable="false"
            show-icon
          >
          </el-alert>
          <el-col :span="24">
            <el-col :span="24" class="table-from">
              <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="标题">
                  <el-input v-model="formInline.title"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="onSubmitTitle" icon="el-icon-search">查询</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-table
              ref="multipleTable"
              :data="tableData.slice((pages.pageNum-1)*pages.pageSize,pages.pageNum*pages.pageSize)"
              tooltip-effect="dark"
              style="width: 100%"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                type="selection"
              >
              </el-table-column>
              <el-table-column label="序号" type="index" width="200"></el-table-column>

              <el-table-column
                label="标题"
              >
                <template slot-scope="scope">{{ scope.row.title }}</template>
              </el-table-column>
              <el-table-column
                prop="name"
                label="作者"
              >
              </el-table-column>
              <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                  <div>
                    <el-button
                      size="mini"
                      @click="handleEdit(scope.row)"
                    >编辑
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <el-dialog title="编辑" :visible.sync="dialogVisible">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="标题" prop="title">
                  <el-input type="text" v-model="ruleForm.title"></el-input>
                </el-form-item>
                <el-form-item label="作者" prop="name">
                  <el-input type="text" v-model="ruleForm.name"></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
              </span>
            </el-dialog>
            <Pagination :total="total" :pageNum.sync="pages.pageNum"
                        :limit.sync="pages.pageSize"
                        :pageSizes="pageSizes"
            ></Pagination>
          </el-col>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from '@/components/Pagination/Pagination'
export default {
  name: 'inline',
  components:{Pagination},
  data() {
    return {
      formInline: {
        title: ''
      },
      ruleForm: {
        name: '',
        title: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      pageSizes: [10, 20, 30, 40, 50, 100],
      pages: {
        pageNum: 1,
        pageSize: 20
      },
      tableData: [],
      dataList: [],
      total: 0,
      loading: false
    }
  },
  methods: {
    fetchData() {
      //请求接口加载表格数据
      this.loading = true
      axios.get('http://route.showapi.com/60-27').then(res => {
        this.tableData = res.data._data.data.rows
        this.dataList = res.data._data.data.rows
        this.$store.commit('user/saveData', res.data._data.data.rows)
        this.total = res.data._data.data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          });
        } else {
          this.$message.error('error submit!!');
          return false;
        }
      });
    },
    handleEdit( row) {
      this.ruleForm.title = row.title
      this.ruleForm.name = row.name
      this.dialogVisible = true
    },
    onSubmitTitle() {
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].title.indexOf(this.formInline.title) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    },
  },
  mounted() {
    this.fetchData()
  }

}
</script>

<style scoped lang="scss">
.box {
  .table-from {
    padding: 20px 10px;
  }
  ::v-deep.el-icon-search {
    font-size: 14px;
  }
  ::v-deep.cell {
    font-size: 14px;
    font-weight: 400;
    text-align: center;
  }

  ::v-deep.el-form-item__label {
    font-weight: 400;
  }
}
</style>
